package com.excilys.formation.projet.ebanking.webapp.security;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.excilys.formation.projet.ebanking.modele.Utilisateur.eRole;

public class RedirectLogin implements AuthenticationSuccessHandler {
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		
		GrantedAuthority role = ((List<GrantedAuthority>) authentication.getAuthorities()).get(0);
		
		if (role.getAuthority().equals(eRole.ROLE_CLIENT.toString())) {
			response.sendRedirect("client/index.html");
			
		}
		else if (role.getAuthority().equals(eRole.ROLE_ADMIN.toString())) {
			response.sendRedirect("admin/index.html");
		}
		else {
			response.sendRedirect("login.html");
		}
		
	}
	
}
